/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import Modelo.DAL.Implemento_DAO;
import Acceso.Security;
import java.io.PrintWriter;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Juan Reyes
 */
public class consultarImplementoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String tipo = request.getParameter("tipo");
        String subtipo = request.getParameter("subtipo");
        String marca = request.getParameter("marca");
        String calificacion = request.getParameter("calificacion");
        int calificacionInt;
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");

        if (calificacion.equals("")) {
            calificacionInt = -1;
        } else {
            calificacionInt = Integer.parseInt(calificacion);
        }

        String atributos = " * ";
        String from = " from implemento ";
        String select = "Select ";
        String selectCount = "Select count(*) ";
        String where = " where 1 = 1 ";
        String limite = "";


        if (tipo != null && !tipo.equals("")) {
            where += " and nombre = '" + tipo + "'";
        }

        if (subtipo != null && !subtipo.equals("")) {
            where += " and subtipo = '" + subtipo + "'";
        }

        if (marca != null && !marca.equals("")) {
            where += " and lower(marca) like lower('%" + marca + "%')";
        }

        if (calificacionInt != -1) {
            where += " and calificacion = " + calificacionInt;
        }

        if (!limit.equals("0")) {
            limite += " limit " + limit + " offset " + offset;
        }

        if (order != null && !order.equals("")) {
            select += atributos + ", ( " + selectCount + from + where + " ) " + from + where + order + limite + ";";
        } else {
            select += atributos + ", ( " + selectCount + from + where + " ) " + from + where + limite + ";";
        }

        response.setContentType("text/json");
        System.out.println("\n\n\n\tWHERE: " + where);
        JSONArray implemento = Implemento_DAO.consultarImplemento(select);
        if (implemento == null) {
            return mapping.findForward(FAILURE);
        }
        JSONObject ext = new JSONObject();
        ext.put("consultarImplemento", implemento);
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();


        return mapping.findForward(SUCCESS);
    }
}
